;(function($){
	$.dialog = function(opt){
		if(opt.id && $("#"+opt.id).length > 0){
			openDialog(opt);
		}else{
			var settings = {
					id:'modal-', //modal id
					title:'标题',
					url:'',  //url
					type:1,//1、iframe弹出框，2、alert提示框，3、confirm确认框
					width:300,
					height:300,
					confirm:null,
					cancel:null
				};
			settings = $.extend(settings, opt);
			init(settings);
			openDialog(settings);		
			
		}
		
		function openDialog(settings){
			switch(settings.type){
				case 1:
					if($('#'+settings.id).find("iframe").attr("src") != settings.url){
						$('#'+settings.id).find("iframe").attr("src",settings.url);
						setTimeout(function(){
							$('#'+settings.id).modal({width:settings.width, height:settings.height, closeViaDimmer:0});
						}, 300);
					}else{
						$('#'+settings.id).modal({width:settings.width, height:settings.height, closeViaDimmer:0});
					}									
					break;
				case 2:
					$('#'+settings.id).modal();
					break;
				case 3:
					$('#'+settings.id).modal({
				        relatedTarget: this,
				        onConfirm: settings.confirm,
				        onCancel: settings.cancel
				    });
					break;
				default:
			}
		}
		function init(opt){
			switch(opt.type){
				case 1:
					init1(opt);										
					break;
				case 2:
					init2(opt);
					break;
				case 3:
					init3(opt);
					break;
				default:
			}
		}
		
		function init1(opt){			
			var h = [];
			h.push('<div class="am-modal am-modal-no-btn" tabindex="-1" id="'+settings.id+'">');
			h.push('<div class="am-modal-dialog">');
			h.push('<div class="am-modal-hd">'+settings.title);
			h.push('<a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>');
			h.push('</div>');		
			h.push('<div class="am-modal-bd" style="padding:0;">');
			h.push('<iframe src="'+settings.url+'" width="100%" height="100%"></iframe>');
			h.push('</div></div></div>');
			
			$("body").append(h.join(""));			
		}
		function init2(opt){			
			var h = [];
			h.push('<div class="am-modal am-modal-alert" tabindex="-1" id="'+settings.id+'">');
			h.push('<div class="am-modal-dialog">');
			h.push('<div class="am-modal-hd"></div>');	
			h.push('<div class="am-modal-bd">'+settings.title+'</div>');
			h.push('<div class="am-modal-footer"><span class="am-modal-btn">确定</span></div>');
			h.push('</div></div>');
			
			$("body").append(h.join(""));			
		}
		function init3(opt){			
			var h = [];
			h.push('<div class="am-modal am-modal-confirm" tabindex="-1" id="'+settings.id+'">');
			h.push('<div class="am-modal-dialog">');
			h.push('<div class="am-modal-hd"></div>');	
			h.push('<div class="am-modal-bd">'+settings.title+'</div>');
			h.push('<div class="am-modal-footer">');
			h.push('<span class="am-modal-btn" data-am-modal-cancel>取消</span>');
			h.push('<span class="am-modal-btn" data-am-modal-confirm>确定</span>');
			h.push('</div></div></div>');
			
			$("body").append(h.join(""));			
		}
	};
})(jQuery);
